package com.example.demo.web;

import com.example.demo.param.dto.response.InsertResponseDto;
import com.example.demo.param.dto.response.UpdateResponseDto;
import com.example.demo.param.vo.FollowUserVO;
import com.example.demo.param.vo.ResponseVO;
import com.example.demo.service.IResponseService;
import com.example.demo.service.ITokenService;
import com.example.demo.util.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "回答问题")
@RestController
@RequestMapping("response")
public class ResponseController extends BaseController{

    @Autowired private IResponseService responseService;
    @Autowired private ITokenService tokenService;

    @ApiOperation("新增回答")
    @PostMapping("insert")
    public Json<Boolean> insert (@RequestBody InsertResponseDto dto) {
        String uid = this.tokenService.verification(dto.getToken());
        Boolean exit = this.responseService.insert(uid,dto.getQuestionId(),dto.getResponseContent());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("删除回答")
    @DeleteMapping("delete")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
    })
    public Json<Boolean> delete (@RequestParam String id, @RequestParam String token){
        this.tokenService.verification(token);
        Boolean exit = this.responseService.delete(id);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("修改回答")
    @PostMapping("update")
    public Json<Boolean> update(@RequestBody UpdateResponseDto dto){
        String uid = this.tokenService.verification(dto.getToken());
        Boolean exit = this.responseService.update(dto.getUserId(),dto.getResponseContent());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("查找我的所有回答")
    @GetMapping("getMyRes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "展示的页数", required = true, paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示几条", required = true, paramType = "query", dataType = "int", example = "10")
    })
    public Json<ResponseVO> getMyRes(@RequestParam String token,
                                     @RequestParam Integer page,
                                     @RequestParam Integer pageSize){
        String uid = this.verification(token);
        return new Json<ResponseVO>(this.responseService.getMyRes(uid,page,pageSize));
    }
}
